/*
 * @Description: 
 * @Version: 0.1
 * @Autor: 许振富 zfgj201511
 * @Date: 2021-11-30 16:05:09
 * @FilePath: \mt_current\User\main.c
 * @LastEditors: 许振富(jia)
 * @LastEditTime: 2024-07-24 23:13:20
 */

#include "stm32f10x.h"

#include "hw_gpiomap.h"
#include "ASM_Delay.h"
#include "../System/System_Init/System_Init.h"
#include "../System/Protocol/Protocol.h"
#include "../System/Protocol/cmd_process.h"
#include "../System/Soft_Timer/Soft_Timer.h"
#include "../System/hlk_protocol/zw0906_cmd.h"

#include "../User_Drivers/Bsp_Key/Bsp_Key.h"
#include "../System/ASM_Delay/ASM_Delay.h"
#include "RTT_Log.h"


void _zw0906_loop(void);
void key_loop(void);
int main(void)
{
  uint16_t each = 0;
  unsigned char Key_Status = 0;
  Set_System();

  while (1)
  {
	  _zw0906_loop();
	  key_loop();
    // 检查协议栈是否接收到数据
    if (Protocol_GetValid())
    {
      // 协议栈接收到完整数据包
      do
      {
        uint16_t each = 0;
        for (; each < MAX_CMD_NUMBER; each++)
        {
          // 在已注册的命令中寻找匹配的指令
          if (Protocol_Rx_Buffer.Cmd == CMD_HANDLER_List[each].Cmd)
          {
            // 调用对应命令的服务函数
            CMD_HANDLER_List[each].Handler(&Protocol_Rx_Buffer);
            break;
          }
        }
        // 若未找到匹配的命令
        if (each == MAX_CMD_NUMBER)
          Protocol_RePort(0x0001, (void *)0, 0);
      } while (0);
    }
  }
}


void _zw0906_loop(void)
{
	unsigned char st = 0;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 1)
	{
		if(	zw0906_ps_controlbln(&st,4) != 1)
		{
			return;
		}
		if(zw0906_check() == 0)
		{
			zw0906_ps_controlbln(&st,3);
			GPIO_WriteBit(GPIOC,GPIO_Pin_13,0);
			TIM_SetCompare4(TIM4,500);
		}
		else
		{
			zw0906_ps_controlbln(&st,2);
			GPIO_WriteBit(GPIOC,GPIO_Pin_13,0);
			ASM_DELAY_MS(50);
			GPIO_WriteBit(GPIOC,GPIO_Pin_13,1);
			zw0906_ps_controlbln(&st,5);
			return;
		}
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 1);

		zw0906_ps_controlbln(&st,5);
		GPIO_WriteBit(GPIOC,GPIO_Pin_13,1);
		TIM_SetCompare4(TIM4,1500);
	}
}

void key_loop(void)
{
	switch(Key_Get())
	{
		case(KEY_1_DOWN):
			GPIO_WriteBit(GPIOC,GPIO_Pin_13,0);
			TIM_SetCompare4(TIM4,500);
			while(Key_GetState(0) == 1);
			GPIO_WriteBit(GPIOC,GPIO_Pin_13,1);
			TIM_SetCompare4(TIM4,1500);
			break;
		case(KEY_1_UP):
			break;
		default:break;
	}
}





